package com.zhida.jiyangcenter.modle

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.getStatusBarHeight
import chooongg.base.utils.resColor
import chooongg.base.utils.startActivity
import chooongg.base.utils.tipFail
import chooongg.base.widget.recyclerView.divider.HorizontalDividerItemDecoration
import chooongg.base.widget.recyclerView.divider.VerticalDividerItemDecoration
import chooongg.http.APIException
import com.amap.api.location.AMapLocationClient
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIExamination
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.StoreExaminationListEntity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.getLocation
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_healthy_test.*

@TitleBar(false)
@ContentLayout(R.layout.activity_healthy_test)
class HealthyTestActivity : BaseActivity() {
    val adapter = Adapter()
    val page = 1
    var city = ""

    //声明AMapLocationClient类对象
    private lateinit var locationClient: AMapLocationClient

    override fun initConfig(savedInstanceState: Bundle?) {
        val window = activity.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        view_status_bar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = getStatusBarHeight()
        }
        rv_store.layoutManager = object : LinearLayoutManager(context) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rv_store.adapter = adapter
        rv_store.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(context)
                .sizeResId(R.dimen.marginContentSmall).colorResId(R.color.colorBack).build()
        )
        adapter.setOnItemClickListener { _, view, position ->
            startActivity<PhysicalExaminationActivity> {
                putExtra("uuid", adapter.data[position].uuid)
                putExtra("city", city)
            }
        }
        locationClient = getLocation(context) {
            city = it.city
            getData(it.city, it.longitude, it.latitude)
        }
        tv_commit.setOnClickListener {
            startActivity<UpdataReportActivity> { }
        }
    }

    override fun onResume() {
        super.onResume()
        locationClient.startLocation()

    }

    override fun onPause() {
        locationClient.stopLocation()
        super.onPause()
    }

    override fun onDestroy() {
        locationClient.onDestroy()
        super.onDestroy()
    }

    private fun getData(city: String, longitude: Double, latitude: Double) {
        lifecycleScope.request({
            APIExamination.service().examinationList(city, longitude, latitude, page, "")
        },
            object : BaseAPI<StoreExaminationListEntity>() {
                override fun onSuccess(t: StoreExaminationListEntity?, state: Int, msg: String) {
                    adapter.setNewData(t?.serve)
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }

            })
    }

    class Adapter :
        BaseQuickAdapter<StoreExaminationListEntity.Serve, BaseViewHolder>(R.layout.item_healthy_test) {
        override fun convert(helper: BaseViewHolder, item: StoreExaminationListEntity.Serve?) {
            glideImage(helper.getView(R.id.civ_fragment_main_healthy), item?.picture ?: "")
            helper.setText(R.id.tv_healthy_title, item?.name)
                .setText(R.id.tv_healthy_money, "￥" + item?.price)
                .setText(R.id.tv_healthy_name, item?.store_name)
                .setText(R.id.tv_healthy_address, item?.distance + "km")
            val rv_store_tab = helper.getView<RecyclerView>(R.id.rv_healthy_tab)
            rv_store_tab.layoutManager =
                object : LinearLayoutManager(mContext, HORIZONTAL, false) {
                    override fun canScrollHorizontally(): Boolean {
                        return false
                    }
                }
            rv_store_tab.adapter = TabAdapter().apply {
                setNewData(item?.subtitle?.split(" "))
            }
            rv_store_tab.addItemDecoration(
                VerticalDividerItemDecoration.Builder(mContext)
                    .sizeResId(R.dimen.d4).colorResId(R.color.white).build()
            )
            val rv_store_project = helper.getView<RecyclerView>(R.id.rv_healthy_project)
            rv_store_project.layoutManager = object : LinearLayoutManager(mContext) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            rv_store_project.adapter = ProjectAdapter().apply {
                setNewData(item?.description?.split("，"))
            }
            rv_store_project.addItemDecoration(
                HorizontalDividerItemDecoration.Builder(mContext)
                    .sizeResId(R.dimen.d4).colorResId(R.color.white).build()
            )


        }

        class TabAdapter :
            BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_fragment_main_store_tab) {
            override fun convert(helper: BaseViewHolder, item: String?) {
                helper.setText(R.id.tv_tab, item)
                    .setBackgroundRes(R.id.tv_tab, R.drawable.bg_fill_f1efef100)
                    .setTextColor(R.id.tv_tab, mContext.resColor(R.color.textGray))
            }

        }

        class ProjectAdapter :
            BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_fragment_main_store_tab) {
            override fun convert(helper: BaseViewHolder, item: String?) {
                helper.setText(R.id.tv_tab, item)
                    .setBackgroundRes(R.id.tv_tab, R.drawable.bg_empty_blue8)
                    .setTextColor(R.id.tv_tab, mContext.resColor(R.color.textColor))
            }

        }
    }
}